package com.rachel.studyapp.adapter;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.rachel.commonsdk.glide.GlideManager;
import com.rachel.studyapp.R;
import com.rachel.studyapp.entries.HomePagerBean;
import com.rachel.studyapp.json.homejson.Datalist;
import com.rachel.studyapp.utils.ToolUtils;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class LBaseAdapter extends BaseAdapter {
	private static final String TAG = "zsr";
	private Activity mContext;
	private List<Datalist> mList;
	private BasePagerAdapter mPagerAdapter = null;
	private List<HomePagerBean> mPagerBeans = new ArrayList<>();
	private ViewPager mViewPager;
	private JCVideoPlayerStandard mCusVideoView = null;
	private ListView mListView;
	private boolean isPause;
	private String url = "http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4";
	public LBaseAdapter(Activity context, List<Datalist> list){
		
		mContext = context;
		mList = list;

	}
	public void setListview(ListView listview){
		mListView = listview;
		if (mListView != null){
			mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
				@Override
				public void onScrollStateChanged(AbsListView view, int scrollState) {

				}

				@Override
				public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
					if (mCusVideoView != null) {
						int[] loaction = new int[2];
						mCusVideoView.getLocationOnScreen(loaction);
						int moveheight = loaction[1];
						int parentheight = mContext.getResources().getDisplayMetrics().heightPixels;
						int selfheight = mCusVideoView.getHeight();
						//当前为播放状态才会执行这一步
						if (mCusVideoView.currentState == JCVideoPlayer.CURRENT_STATE_PLAYING) {
							if (moveheight <= 0 || moveheight > (parentheight - selfheight)) {
								if (!isPause) {
									isPause = true;
									mCusVideoView.startButton.performClick();

								}
							} else {
								if (isPause) {
									isPause = false;
									//mCusVideoView.startButton.performClick();
								}
							}
						}
					}
				}
			});
		}
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}
	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return mList.get(arg0);
	}
	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public int getItemViewType(int position) {
		return mList.get(position).getType();
	}

	@Override
	public int getViewTypeCount() {
		return 4;
	}



	@Override
	public View getView(final int position, View contentView, ViewGroup parent) {


		int viewtype = getItemViewType(position);
		Datalist bean = mList.get(position);

		LBaseViewholder viewholder = null;
		switch (viewtype){
			case ToolUtils.TYPE_ZERO:

				viewholder = LBaseViewholder.getLBaseViewholder(mContext,
						contentView, R.layout.home_item_list_video,parent,position);

				if (mCusVideoView == null){
					mCusVideoView = viewholder.getView(R.id.item_video_view);
					mCusVideoView.setUp(url,JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL,
							"万万没想到，客户竟然是这种人");
					mCusVideoView.thumbImageView.setImageResource(R.mipmap.previewimg);

				}
				viewholder.setText(R.id.item_title_view,bean.getTitle())
						.setText(R.id.item_info_view,mContext.getString(R.string.time,bean.getInfo()))
						.setText(R.id.item_text_view,bean.getText())
						.setDrawable(R.id.item_logo_view,bean.getLogo(),true);

				break;
			case ToolUtils.TYPE_ONE:

				viewholder = LBaseViewholder.getLBaseViewholder(mContext,
						contentView,R.layout.home_item_list_type2,parent,position);
				viewholder.setText(R.id.conment_author_title, bean.getTitle())
						.setText(R.id.conment_author_info,mContext.getString(R.string.time,bean.getInfo()))
						.setText(R.id.conment_course_price,bean.getPrice())
						.setText(R.id.conment_course_text,bean.getText())
						.setText(R.id.conment_course_from,bean.getFrom())
						.setText(R.id.conment_trend_zan,mContext.getString(R.string.trend,bean.getZan()))
						.setDrawable(R.id.conment_author_logo,bean.getLogo(),true);
				LinearLayout imagely = viewholder.getView(R.id.image_ly);
				//动态添加图片
				setImageAuto(imagely,bean.getUrls());


				break;
			case ToolUtils.TYPE_TWO:
				viewholder = LBaseViewholder.getLBaseViewholder(mContext,
						contentView,R.layout.home_item_list_type1,parent,position);
				viewholder.setText(R.id.conment_author_title, bean.getTitle())
						.setText(R.id.conment_author_info,mContext.getString(R.string.time,bean.getInfo()))
						.setText(R.id.conment_course_price,bean.getPrice())
						.setText(R.id.conment_course_text,bean.getText())
						.setText(R.id.conment_course_from,bean.getFrom())
						.setText(R.id.conment_trend_zan,mContext.getString(R.string.trend,bean.getZan()))
						.setDrawable(R.id.conment_author_logo,bean.getLogo(),true)
						.setDrawable(R.id.conment_course_url,bean.getUrls().get(0),false);

				break;
			case ToolUtils.TYPE_THREE:

				viewholder = LBaseViewholder.getLBaseViewholder(mContext,
						contentView, R.layout.home_item_list_type3,parent,position);


				if (mPagerBeans.isEmpty()){

					mPagerBeans.addAll(ToolUtils.getPageTransData(bean));
				}


				if (mPagerAdapter == null){
					mPagerAdapter = new BasePagerAdapter(mContext,mPagerBeans);
				}
				if (mViewPager == null) {
					mViewPager = viewholder.getView(R.id.type3_viewpager);
					mViewPager.setAdapter(mPagerAdapter);
					mViewPager.setPageMargin(ToolUtils.dip2px(mContext,10));
					mViewPager.setPageTransformer(false,new ZoomOutPageTransformer());
					mViewPager.setCurrentItem(mPagerBeans.size()+BasePagerAdapter.RANG_COUNT/2);
					mViewPager.setOffscreenPageLimit(3);
				}

				break;
		}
		return viewholder.getConverView();
		
	}

	private void setImageAuto(LinearLayout imagely, List<String> urls) {
		if (imagely != null){
			imagely.removeAllViews();
		}else{
			imagely.setGravity(Gravity.CENTER);
			return ;
		}
		for (int i = 0; i < urls.size(); i++) {

			String url = urls.get(i);
			ImageView imageView = new ImageView(mContext);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					ToolUtils.dip2px(mContext,100), ViewGroup.LayoutParams.MATCH_PARENT);
			if (i > 0) {
				params.setMargins(ToolUtils.dip2px(mContext, 10), 0, 0, 0);
			}

			imageView.setLayoutParams(params);
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			new GlideManager.Builder()
					.setContext(mContext)
					.setTargetSource(url)
					.setImageView(imageView)
					.builder();
			imagely.addView(imageView);


		}
	}


	/**
	 * 魅族效果
	 */
	class ZoomOutPageTransformer implements ViewPager.PageTransformer {
		private static final float MAX_SCALE = 1.0f;
		private static final float MIN_SCALE = 0.9f;//0.85f
		@Override
		public void transformPage(View view, float position) {
			//setScaleY只支持api11以上
			if (position < -1) {
				// view.setScaleX(MIN_SCALE);
				view.setScaleY(MIN_SCALE);
			} else if (position <= 1) //a页滑动至b页 ； a页从 0.0 -1 ；b页从1 ~ 0.0
			{ // [-1,1]
//              Log.e("TAG", view + " , " + position + "");
				float scaleFactor = MIN_SCALE + (1 - Math.abs(position)) * (MAX_SCALE - MIN_SCALE);
				//  view.setScaleX(scaleFactor);
				//每次滑动后进行微小的移动目的是为了防止在三星的某些手机上出现两边的页面为显示的情况
				view.setScaleY(scaleFactor);
			} else { // (1,+Infinity]
				// view.setScaleX(MIN_SCALE);
				view.setScaleY(MIN_SCALE);
			}
		}
	}

}